﻿using System;
using System.IO;
using System.Text;

namespace Peer
{
    public readonly struct PeerRequest
    {
        internal PeerRequest(BaseConverter converter, Guid id, Stream data)
        {
            this.converter = converter;
            this.Id = id;
            this.Data = data;
        }


        public readonly Stream Data;

        internal readonly BaseConverter converter;

        internal readonly Guid Id;

        public PeerResponse CreateResponse()
        {
            return new PeerResponse(converter, Id, PackageType.Response, null);
        }

        public PeerResponse CreateResponse(Stream data)
        {
            return new PeerResponse(converter, Id, PackageType.ResponseData, data);
        }

        internal PeerResponse CreateResponse(Exception exception)
        {
            exception = (exception.InnerException ?? exception);
            var data = converter.Serialize(exception);
            return new PeerResponse(converter, Id, PackageType.ResponseError, data);
        }
    }
}
